<?php
/***************************************************************************
*                                register.php
 *
 *   Begin date           	: 
 *   Copyright            	: 
 *   Email                	: 
 ***************************************************************************/
defined('PHP5_PHP') or die("Application is stopping!!!");
	require_once($php5RootPath . "/classes/user.class.php");
	require_once($php5RootPath . "/includes/send_email.php");
	$row = new php5User($php5DB);
	
	$task 		= php5GetParam($_REQUEST, 'task', '');
	
	//check task
	$msg = '';
	$noerr = 1;
	//type
	$arrTemp = $define->USER_TYPE();
	$types = array();
	$types[0] = "---";
	foreach($arrTemp as $k => $v) {
		if($k < 2) continue;
		$types[$k] = $v;
	}
	
	switch($task)
	{
		case 'update':
			$row->bind( $_POST );
			if(!trim($row->username)) {
				$noerr = 0;
				echo '<div class="alert alert-danger">'.sprintf($lang['_BLANK_MSG_'], 'Login name').'</div>';
				break;
			}
			if( preg_match("/^([a-zA-Z0-9])+$/", $row->username )==false) {
				$noerr = 0;
				echo '<div class="alert alert-danger">'. sprintf($lang['_INVALID_MSG_'], 'Login name') .'</div>';
				break;
			}
			$where = array();
			$where[] = "username = '".mysql_real_escape_string($row->username)."'";
			$query = "SELECT COUNT(*) "
			. "\n FROM #__users "
			. ( count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : "" )
			;
			
			$php5DB->setQuery( $query );
			$count = $php5DB->loadResult();	
			if( $count) {
				$noerr = 0;
				echo '<div class="alert alert-danger">Username is existed</div>';
				break;
			}		
			if(!trim($row->name)) {
				$noerr = 0;
				echo '<div class="alert alert-danger">'.sprintf($lang['_BLANK_MSG_'], 'Name').'</div>';
				break;
			}
			$newpassword 		= trim(php5GetParam($_REQUEST, 'newpassword', ''));
			$retypepassword 		= trim(php5GetParam($_REQUEST, 'retypepassword', ''));
			if(!$newpassword) {
				$noerr = 0;
				echo '<div class="alert alert-danger">'.sprintf($lang['_BLANK_MSG_'], 'Password').'</div>';
				break;			
			}
			if($newpassword != $retypepassword) {
				$noerr = 0;
				echo '<div class="alert alert-danger">'.$lang['_MSS_004_'].'</div>';
				break;
			}
			
			if(!trim($row->email)) {
				$noerr = 0;
				echo '<div class="alert alert-danger">'. sprintf($lang['_BLANK_MSG_'], 'Email').'</div>';
				break;
			}
			
			if( preg_match("/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/", $row->email )==false) {
				$noerr = 0;
				echo '<div class="alert alert-danger">'.sprintf($lang['_INVALID_MSG_'], 'Email').'</div>';
				break;
			}
			
			$where = array();
			$where[] = "email = '".mysql_real_escape_string($row->email)."'";
			$query = "SELECT COUNT(*) "
			. "\n FROM #__users "
			. ( count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : "" )
			;
			
			$php5DB->setQuery( $query );
			$count = $php5DB->loadResult();	
			if( $count) {
				$noerr = 0;
				echo '<div class="alert alert-danger">'.$lang['_MSS_003_'].'</div>';
				break;
			}
			
			if( !is_valid_date($row->birthday) ) {
				$noerr = 0;
				echo '<div class="alert alert-danger">'. sprintf($lang['_INVALID_MSG_'], 'Birthday').'</div>';
				break;
			}		
						
			//$row->password = md5($newpassowrd);
			//$rand = rand(1000, 999999);
			$row->password = md5($newpassword);
			$row->show_pass = $newpassword;
			$row->store();
			
			if($noerr) {
				$query = "REPLACE INTO #__active_user"
			. "\n SET id='%s', user_id='%d'"
			;
				$key = md5(time());
				$php5DB->setQuery( sprintf($query, $key, $row->id) );
				$php5DB->query();
				$link = $php5WebPath . '/active.php?task=token&id=' . $key;
				
				if (!$row->store()) {
					$noerr = 0;
					echo '<div class="alert alert-danger">Error!!! Please try again!!!</div>';
				} else {
					$body = nl2br(sprintf($lang['_MSS_002_'], $row->name, $row->username, $row->show_pass, $link));
					
					send_email_all($row->email, $lang['_MSS_001_'], $body);
				}
				
			}
			break;
		default:
			break;	
			
	}
	if($noerr)
		echo "done";

